<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2009-2010 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Albums.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Model_DbTable_Albums extends Engine_Db_Table {
	
	protected $_rowClass = 'Sitepage_Model_Album';
	
	public function getSpecialAlbum(Sitepage_Model_Page $page, $type) {
		
		if (! in_array ( $type, array ('note', 'overview', 'wall' ) )) {
			throw new Sitepage_Model_Exception ( 'Unknown special album type' );
		}
		$page_id = $page->page_id;
		$select = $this->select ()->where ( 'page_id = ?', $page_id )->where ( 'type = ?', $type )->order ( 'album_id ASC' )->limit ( 1 );
		$album = $this->fetchRow ( $select );
		
		//CREATE PHOTOS ALBUM IF IT DOESN't EXIST YET
		if (null === $album) {
			$album = $this->createRow ();
			$album->default_value = 0;
			$album->page_id = $page_id;
			$album->owner_id = $page->owner_id;
			$album->title = Zend_Registry::get ( 'Zend_Translate' )->_ ( ucfirst ( $type ) . ' Photos' );
			$album->type = $type;
			if ($type == 'message') {
				$album->search = 0;
			} else {
				$album->search = 1;
			}
			$album->save ();
		}
		return $album;
	}
	
	/**
	 * Gets album for the pages
	 *
	 * @param array params
	 * @return albums for pages
	 */
	public function getAlbums($params = array()) {
		
		$select = $this->select ();
		
		if (isset ( $params ['getSpecialField'] ) && ! empty ( $params ['getSpecialField'] )) {
			$select->from ( $this->info ( 'name' ), array ('count(*) as count', 'title', 'album_id' ) );
		} else {
			$select->from ( $this->info ( 'name' ), array ('album_id', 'page_id', 'owner_id', 'order', 'title', 'search', 'photo_id', 'view_count', 'comment_count', 'default_value', 'like_count', 'type', 'modified_date', 'creation_date' ) );
		}
		
		if (isset ( $params ['page_id'] ) && ! empty ( $params ['page_id'] )) {
			$select->where ( $this->info ( 'name' ) . '.page_id' . '= ?', $params ['page_id'] );
		}
		
		if (isset ( $params ['default_value'] ) && ! empty ( $params ['default_value'] )) {
			$select->where ( $this->info ( 'name' ) . '.default_value' . '= ?', 1 );
		}
		
		$select->order ( $this->info ( 'name' ) . '.album_id ASC' );
		
		if ((isset ( $params ['orderby'] ) && ! empty ( $params ['orderby'] ))) {
			$select->order ( $params ['orderby'] );
		}
		if (isset ( $params ['getSpecialField'] ) && ! empty ( $params ['getSpecialField'] )) {
			$select->group ( $this->info ( 'name' ) . '.album_id' );
		}
		
		if ((isset ( $params ['start'] ) && ! empty ( $params ['start'] )) || (isset ( $params ['end'] ) && ! empty ( $params ['end'] ))) {
			if (! isset ( $params ['end'] )) {
				$params ['end'] = '';
			}
			$select->limit ( $params ['start'], $params ['end'] );
		}
		
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Gets photo id by order
	 *
	 * @param int $page_id 
	 * @return photo ids
	 */
	public function getPageAlbumsOrder($page_id) {
		$currentOrder = $this->select ()->from ( $this->info ( 'name' ), 'photo_id' )->where ( 'page_id = ?', $page_id )->order ( 'order ASC' )->query ()->fetchAll ( Zend_Db::FETCH_COLUMN );
		return $currentOrder;
	}
	
	/**
	 * Get Default Album of page
	 *
	 * @param int $page_id
	 * @return default album
	 */
	public function getDefaultAlbum($page_id) {
		$albumselect = $this->select ()->from ( $this->info ( 'name' ), array ('album_id', 'photo_id' ) )->where ( 'page_id = ?', $page_id )->where ( 'default_value = ?', 1 );
		return $this->fetchRow ( $albumselect );
	}

}

?>